{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.feature_selection import VarianceThreshold,SelectKBest\n",
    "from sklearn.feature_selection import f_regression\n",
    "from sklearn.feature_selection import chi2\n",
    "from sklearn.feature_selection import RFE\n",
    "from sklearn.feature_selection import SelectFromModel\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVR\n",
    "from sklearn.ensemble import GradientBoostingClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = np.array([\n",
    "    [0, 2, 0, 3],\n",
    "    [0, 1, 4, 3],\n",
    "    [0.1, 1, 1, 3]\n",
    "], dtype=np.float64)\n",
    "Y = np.array([1,2,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "VarianceThreshold(threshold=0.1)\n",
      "[[ 2.  0.]\n",
      " [ 1.  4.]\n",
      " [ 1.  1.]]\n"
     ]
    }
   ],
   "source": [
    "variance = VarianceThreshold(threshold=0.1)\n",
    "print variance\n",
    "variance.fit(X)\n",
    "print variance.transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SelectKBest(k=2, score_func=<function f_regression at 0x000000000A8A4BA8>)\n",
      "[  0.33333333   0.33333333  16.33333333          nan]\n",
      "[[ 2.  0.]\n",
      " [ 1.  4.]\n",
      " [ 1.  1.]]\n"
     ]
    }
   ],
   "source": [
    "sk1 = SelectKBest(f_regression, k=2)\n",
    "sk1.fit(X, Y)\n",
    "print sk1\n",
    "print sk1.scores_\n",
    "print sk1.transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SelectKBest(k=2, score_func=<function chi2 at 0x000000000A8A4B38>)\n",
      "[ 0.05   0.125  4.9    0.   ]\n",
      "[[ 2.  0.]\n",
      " [ 1.  4.]\n",
      " [ 1.  1.]]\n"
     ]
    }
   ],
   "source": [
    "sk2 = SelectKBest(chi2, k=2)\n",
    "sk2.fit(X, Y)\n",
    "print sk2\n",
    "print sk2.scores_\n",
    "print sk2.transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True  True False]\n",
      "2\n",
      "[2 1 1 3]\n",
      "[[ 2.  0.]\n",
      " [ 1.  4.]\n",
      " [ 1.  1.]]\n"
     ]
    }
   ],
   "source": [
    "estimator = SVR(kernel='linear')\n",
    "selector = RFE(estimator, 2, step=1)\n",
    "selector = selector.fit(X, Y)\n",
    "print selector.support_\n",
    "print selector.n_features_\n",
    "print selector.ranking_\n",
    "print selector.transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5.1]\n",
      " [ 4.9]\n",
      " [-6.2]\n",
      " [-5.9]]\n"
     ]
    }
   ],
   "source": [
    "X2 = np.array([\n",
    "    [ 5.1,  3.5,  1.4,  0.2],\n",
    "    [ 4.9,  3. ,  1.4,  0.2],\n",
    "    [ -6.2,  0.4,  5.4,  2.3],\n",
    "    [ -5.9,  0. ,  5.1,  1.8]\n",
    "], dtype=np.float64)\n",
    "Y2 = np.array([0, 0, 2, 2])\n",
    "estimator = LogisticRegression(penalty='l1', C=0.1)\n",
    "sfm = SelectFromModel(estimator)\n",
    "sfm.fit(X2, Y2)\n",
    "print sfm.transform(X2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5.1  0.2]\n",
      " [ 4.9  0.2]\n",
      " [-6.2  2.3]\n",
      " [-5.9  1.8]]\n"
     ]
    }
   ],
   "source": [
    "X2 = np.array([\n",
    "    [ 5.1,  3.5,  1.4,  0.2],\n",
    "    [ 4.9,  3. ,  1.4,  0.2],\n",
    "    [ -6.2,  0.4,  5.4,  2.3],\n",
    "    [ -5.9,  0. ,  5.1,  1.8]\n",
    "], dtype=np.float64)\n",
    "Y2 = np.array([0, 0, 2, 2])\n",
    "estimator = GradientBoostingClassifier()\n",
    "sfm = SelectFromModel(estimator)\n",
    "sfm.fit(X2, Y2)\n",
    "print sfm.transform(X2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.525  1.725  3.325  1.125]\n",
      "[[-0.90319637 -0.24920124  0.31484024  0.15169269]\n",
      " [ 0.03919747 -0.80616444 -0.27357029 -0.52318424]]\n",
      "[[-6.26919501 -0.19988789]\n",
      " [-5.96395512  0.19535484]\n",
      " [ 6.28736349 -0.33667759]\n",
      " [ 5.94578665  0.34121064]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "X2 = np.array([\n",
    "    [ 5.1,  3.5,  1.4,  0.2],\n",
    "    [ 4.9,  3. ,  1.4,  0.2],\n",
    "    [ -6.2,  0.4,  5.4,  2.3],\n",
    "    [ -5.9,  0. ,  5.1,  1.8]\n",
    "], dtype=np.float64)\n",
    "pca = PCA(n_components=2)\n",
    "pca.fit(X2)\n",
    "print pca.mean_\n",
    "print pca.components_\n",
    "print pca.transform(X2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-27.18468036]\n",
      " [-25.44008732]\n",
      " [ 26.80136112]\n",
      " [ 25.82340656]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.discriminant_analysis import LinearDiscriminantAnalysis \n",
    "X2 = np.array([\n",
    "    [ 5.1,  3.5,  1.4,  0.2],\n",
    "    [ 4.9,  3. ,  1.4,  0.2],\n",
    "    [ -6.2,  0.4,  5.4,  2.3],\n",
    "    [ -5.9,  0. ,  5.1,  1.8]\n",
    "], dtype=np.float64)\n",
    "Y2 = np.array([0, 0, 2, 2])\n",
    "lda = LinearDiscriminantAnalysis(n_components=2)\n",
    "lda.fit(X2, Y2)\n",
    "print lda.transform(X2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
